<?php

function getTheatresByZip( $request ) {

  /*
    - $request is a MovieRequest object
    - $response is an array of MovieTheatre objects
  */

  $TAX_RATE = .0925; # Required for the "purchase" class; this is sort of a constant
  
  $ch = 
    curl_init("http://gateway.moviefone.com/movies/rss/closesttheaters.xml?zip=" . $request->zip);
  curl_setopt($ch, CURLOPT_HEADER, false); # The header screws the SimpleXMLElement constructor up
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); # curl_exec will return a string
  $xml = curl_exec($ch);
  curl_close($ch);
  
  try {
    $xmlTree = new SimpleXMLElement( $xml );
  } catch( Exception $e ) {
    echo "An exception was thrown during the creation of the SimpleXMLElement object\n";
    # Needs to display error information
  }

  $x = 0;
  foreach( $xmlTree->channel->item as $item ) {
    /*
      With the current XML feed, item->phone does not contain anything.
      The phone number is wrapped in HTML tags inside of the description so this
      loop will eventually need to account for that.
    */
    $response[$x] = 
      new MovieTheatre( NULL, $item->title, $item->phone, $item->description, NULL );
    $x++;
  }

  # Return an array of MovieTheatre objects
  return $response;

}

?>
